package com.ccnf.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ccnf.core.utils.ResultUtil;
import com.ccnf.core.utils.SystemClock;
import com.ccnf.model.entity.*;
import com.ccnf.model.mapper.*;
import com.ccnf.model.pojo.Report;
import com.ccnf.service.InvitationRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class InvitationRecordServiceImpl extends ServiceImpl<InvitationRecordMapper, InvitationRecord> implements InvitationRecordService {
    @Autowired
    private StarRecordMapper starRecordMapper;
    @Autowired
    private InvitationUserMapper invitationUserMapper;
    @Autowired
    private InvitationRegisterMapper invitationRegisterMapper;
    @Autowired
    private InvitationEnterMapper invitationEnterMapper;
    @Autowired
    private InvitationReportMapper invitationReportMapper;

    @Override
    public List<Map<String, Object>> findTotalRecordByUserId(Long expoId, Long userId, Integer type) {
        return baseMapper.selectTotalCount(expoId, userId, type);
    }

    @Override
    public Page<Map<String, Object>> findRecordsByUserId(Page page, Long expoId, Long userId, Integer recordType, Integer type) {
        return page.setRecords(baseMapper.selectListCount(page, expoId, userId, recordType, type));
    }

    @Override
    public Page<Map<String, Object>> findAllExhibitorRecordsByUserId(Page page, Long expoId, Long userId, Integer type) {
        return page.setRecords(baseMapper.selectAllUserListCount(page, expoId, userId, type));
    }

    @Override
    public void modifyStarStatus(InvitationRecord invitationRecord) {
        if (invitationRecord.getIsStar() == 1) {
            invitationRecord.setStarTime(SystemClock.second());
        }
        ResultUtil.validateSQL(baseMapper.updateStarStatus(invitationRecord), "关注失败");
    }

    @Override
    public void saveUserStar(StarRecord starRecord) {
        StarRecord selectStarRecord = new StarRecord();
        selectStarRecord.setUserId(starRecord.getUserId());
        selectStarRecord.setStarUserId(starRecord.getStarUserId());
        selectStarRecord.setStarType(starRecord.getStarType());
        StarRecord select = starRecordMapper.selectOne(selectStarRecord);
        Integer isStar = starRecord.getIsStar();
        if (select == null && isStar == 1) {
            starRecord.setStarTime(SystemClock.second());
            ResultUtil.validateSQL(starRecordMapper.insert(starRecord), "关注失败");
        } else if (!select.getIsStar().equals(isStar)) {
            if (isStar == 1) {
                select.setStarTime(SystemClock.second());
            }
            select.setIsStar(isStar);
            ResultUtil.validateSQL(starRecordMapper.updateById(select), isStar == 1 ? "" : "取消" + "关注失败");
        }
    }

    @Override
    public void saveInvitationUserRegister(InvitationRegister invitationRegister) {
        Long invitationId = invitationRegister.getInvitationId();
        InvitationUser invitationUser = new InvitationUser();
        invitationUser.setInvitationId(invitationId);
        invitationUser.setUserId(invitationRegister.getUserId());
        invitationUser.setRegisterTime(SystemClock.second());
        invitationUserMapper.updateRegisterTime(invitationUser);

        invitationRegister.setRegisterTime(SystemClock.second());
        invitationRegisterMapper.insert(invitationRegister);

        baseMapper.incrementRegisterCount(invitationId);
        invitationReportMapper.incrementRegisterCount(invitationId, SystemClock.zeroSecond());
    }

    @Override
    public void updateInvitationUserEnter(String registerId) {
        InvitationRegister select = invitationRegisterMapper.selectById(registerId);
        ResultUtil.validateNull(select, "该登记门票没有找到");
        Long invitationId = select.getInvitationId();
        Long userId = select.getUserId();

        InvitationUser invitationUser = new InvitationUser();
        invitationUser.setInvitationId(invitationId);
        invitationUser.setUserId(userId);
        invitationUser.setEnterTime(SystemClock.second());
        invitationUserMapper.updateEnterTime(invitationUser);

        InvitationEnter invitationEnter = new InvitationEnter();
        invitationEnter.setInvitationId(invitationId);
        invitationEnter.setUserId(userId);
        invitationEnter.setRegisterId(registerId);
        invitationEnter.setEnterTime(SystemClock.second());
        invitationEnterMapper.insert(invitationEnter);

        baseMapper.incrementEnterCount(invitationId);
        invitationReportMapper.incrementEnterCount(invitationId, SystemClock.zeroSecond());
    }

    @Override
    public Page<Map<String, Object>> findInvitationUserList(Page page, Long invitationId, Long userId) {
        return page.setRecords(invitationUserMapper.selectInvitationUserList(page, invitationId, userId));
    }

    @Override
    public List<Map<String, Object>> findInvitationAllUserList(Long invitationId, Long userId) {
        return invitationUserMapper.selectInvitationAllUserList(invitationId, userId);
    }

    @Override
    public List<Report> findInvitationReportByInvitationId(Long invitationId, Integer reportType, Integer timeType) {
        long startTime = 0;
        long zeroTime = SystemClock.zeroTime() / 1000;
        if (timeType == 0) {
//            startTime = zeroTime - 2592000;// 从当前零点时间前一个月的时间戳
            startTime = zeroTime - 2505600;// 从当前零点时间前一个月的时间戳
        } else if (timeType == 1) {
//            startTime = zeroTime - 604800;// 从当前时间零点前一个星期的时间戳
            startTime = zeroTime - 518400;// 从当前时间零点前一个星期的时间戳
        }

        // 通过邀请函每日统计数据的报表数据
        List<Report> reports = invitationReportMapper.selectReport(reportType, invitationId, startTime);
        int size = reports.size();
        if (size == 0) {
            // 没有报表数据
            while (startTime <= zeroTime) {
                Report empty = new Report();
                empty.setReportNum(0);
                empty.setCreateTime(startTime);
                reports.add(empty);
                startTime += 86400;
            }
        } else {
            int i = 0;
            while (startTime <= zeroTime) {
                if (size <= i || startTime != reports.get(i).getCreateTime()) {
                    Report empty = new Report();
                    empty.setReportNum(0);
                    empty.setCreateTime(startTime);
                    reports.add(i, empty);
                    size++;
                }
                i++;
                startTime += 86400;
            }
        }
        return reports;
    }

    @Override
    public Page<Map<String, Object>> findInvitationOrientationRank(Long invitationId, Integer type, Integer recordType, Long date, Page page) {
        if (recordType == 1 && date == null) {
            // 日排行
            date = SystemClock.zeroSecond();
        }
        return page.setRecords(baseMapper.selectInvitationOrientationRank(invitationId, type, recordType, date, page));
    }

    @Override
    public Map<String, Object> findInvitationOrientationCurrentUserRank(Long invitationId, Long userId, Integer type, Integer recordType, Long date) {
        if (recordType == 1 && date == null) {
            // 日排行
            date = SystemClock.zeroSecond();
        }
        return baseMapper.selectInvitationOrientationCurrentUserRank(invitationId, userId, type, recordType, date);
    }

    @Override
    public Page<Map<String, Object>> findInvitationRecordRank(Long referenceId, Integer type, Long endTime, Page page) {
        return page.setRecords(invitationReportMapper.selectCountBy(referenceId, endTime, type, page));
    }
}